#include <QtOpenGL>
#include "Object3D.h"
//#include "Global.h"

Object3D::Object3D(const QVector3D& center, QObject *parent)
    : QObject(parent), m_center(center), m_translation(QVector3D()),
      m_rotations(QMatrix4x4()) ,
      m_interactiveQuartenion(QQuaternion()),
      m_slices(50), m_stacks (50), m_texture(-1), m_inputType(GL_TRIANGLES)
{

}

Object3D::Object3D(const Object3D& obj)
    : QObject(obj.parent()), m_center(obj.center()),
      m_translation(obj.translation()), m_rotations(obj.rotations()),
      m_interactiveQuartenion(obj.interactiveQuartenion()),
      m_slices(obj.slices()), m_stacks(obj.stacks()),
      m_texture(obj.texture()), m_inputType(obj.inputType())
{
}

void Object3D::setRotations(const QMatrix4x4& rotations)
{
    m_rotations = rotations;
}

const QMatrix4x4& Object3D::rotations(void)const
{
    return m_rotations;
}

void Object3D::setInteractiveQuartenion(const QQuaternion& quaternion)
{
    m_interactiveQuartenion = quaternion;
}

const QQuaternion& Object3D::interactiveQuartenion(void)const
{
    return m_interactiveQuartenion;
}

void Object3D::setCenter(const QVector3D& center)
{
    m_center = center;
}

const QVector3D& Object3D::center(void)const
{
    return m_center;
}

void Object3D::setSlices(int slices)
{
    m_slices = slices;
}

int Object3D::slices(void)const
{
    return m_slices;
}

void Object3D::setStacks(int stacks)
{
    m_stacks = stacks;
}

int Object3D::stacks(void)const
{
    return m_stacks;
}
void Object3D::setTraslation(const QVector3D& translation)
{
    m_translation = translation;
}

const QVector3D& Object3D::translation(void)const
{
    return m_translation;
}

void Object3D::setTexture(int texture)
{
    m_texture = texture;
}

void Object3D::setColor(int r, int g, int b)
{
    //glColor3i(red,green,blue);
    //glColor3i(255,0,0);
    red = r/255.0;

    green = g/255.0;
    blue = b/255.0;
    if(red > 1)red = 1;
    if(green > 1) green = 1;
    if(blue > 1)blue = 1;
   // float green, blue = 0.0;
     qDebug() << "redddd --- " << red;
  //    this->setCor()
     // glColor3f(r,g,b);


}
void Object3D::setEscala(float xx, float yy, float zz)
{
     if(xx){
          valorX  = xx;
     }else{
       valorX = 1.0;
     }

     if(yy){
          valorY  = yy;
     }else{
       valorY = 1.0;
     }

     if(zz){
          valorZ  = zz;
     }else{
       valorZ = 1.0;
     }

    qDebug() << "x --  "<< valorX;
    qDebug() << "y --  "<<  valorY;
    qDebug() << "z --  "<<  valorZ;
   // glScalef(valorX,valorY,valorZ);

}
void Object3D::setShader(bool ligado) {
   mostraShader = ligado;
}
int Object3D::texture(void)const
{
    return m_texture;

}

void Object3D::addRotation(const QQuaternion& rotation)
{
    QMatrix4x4 m;
    m.rotate(degreeFromCos(rotation.scalar()), rotation.x(), rotation.y(), rotation.z());
    m_rotations = m*m_rotations;
}

void Object3D::draw(void)
{
    glMatrixMode(GL_MODELVIEW_MATRIX);
    glPushMatrix();
    glColor3f(red,green,blue);


    glTranslatef(translation().x(), translation().y(), translation().z());
    glTranslatef(center().x(), center().y(), center().z());
    glScalef(valorX,valorY,valorZ);
    QQuaternion t = interactiveQuartenion();
    glRotatef(degreeFromCos(t.scalar()),
              t.x(), t.y(), t.z());

    glMultMatrixd(rotations().constData());

    drawGeometry();

    glPopMatrix();
}

void Object3D::cleanTransformations()
{
    setTraslation(QVector3D());
    setCenter(QVector3D());

    setInteractiveQuartenion(QQuaternion());
    setRotations(QMatrix4x4());
}

int Object3D::inputType(void)const
{
    return m_inputType;
}

void Object3D::setInputType(int input)
{
    m_inputType = input;
}

double Object3D::degreeFromCos(double _cos)const
{
    return (acos(_cos)*180.0)/M_PI;
}
